package spring.framework.core.io;

import cn.hutool.core.lang.Assert;
import spring.framework.beans.BeansException;

import static cn.hutool.core.util.URLUtil.CLASSPATH_URL_PREFIX;

/**
 * @author fun.pengzh
 * @class spring.framework.core.io.DefaultResourceLoader
 * @desc
 * @since 2021-11-25
 */
public class DefaultResourceLoader implements ResourceLoader {

    @Override
    public Resource getResource(String location) throws BeansException {
        Assert.notBlank(location);
        if (location.startsWith(CLASSPAHT_PREFIX)) {
            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
        } else {
            try {
                return new UrlResource(location);
            } catch (Exception e) {
                return new FileSystemResource(location);
            }
        }
    }
}
